package cn.ewan.cloud.tenant.data.router.mq.core;

import cn.ewan.cloud.common.support.utils.TraceUtil;
import cn.ewan.cloud.common.web.support.rocketmq.BaseRocketMQConsumer;
import cn.ewan.cloud.tenant.data.router.core.business.context.TenantContext;
import cn.ewan.cloud.tenant.data.router.core.common.constants.TenantConstants;
import cn.ewan.cloud.tenant.data.router.core.common.util.TenantLog;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * @author lgh
 * @date 2023/3/1 18:15
 */
public abstract class TenantRocketMQConsumer extends BaseRocketMQConsumer {


    @Override
    public void onMessage(MessageExt message) {
        String tenant = message.getProperty(TenantConstants.TENANT_TAG);
        TenantContext.setTenant(tenant);
        try {
            TraceUtil.setTraceId(message.getProperty("trace_id"));
            TenantLog.log("mq consumer", message.getTopic());
            super.onMessage(message);
        } catch (Exception ex) {
            throw ex;
        } finally {
            TenantContext.removeTenant();
            TenantLog.log("mq consumer finish", message.getTopic());
        }

    }
}
